﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Data.Common;


namespace LightWeightORM
{
    namespace ORM
    {
        namespace DbHelper
        {
            /// <summary>
            /// Summary description for SQLiteHelper
            /// </summary>
            public class SQLiteHelper:IDbHelper
            {
                private string _connectionString;
                private string ConnectionString
                {
                    get { return _connectionString; }
                    set { _connectionString = value; }
                }

                private SQLiteConnection _connection;
                public SQLiteConnection Connection
                {
                    get { return _connection; }
                    set { _connection = value; }
                }

                private SQLiteCommand _command;
                public SQLiteCommand Command
                {
                    get { return _command; }
                    set { _command = value; }
                }

                public SQLiteHelper(string connectionString)
                {
                    this.ConnectionString = connectionString;
                    this.Connection = new SQLiteConnection(connectionString);
                    this.Command = this.Connection.CreateCommand();
                }


                #region IDbHelper Members

                public int Fill(DataTable dataTable, string commandText, params DbParameter[] parms)
                {
                    SQLiteCommand cmd = this.Connection.CreateCommand();
                    cmd.CommandText = commandText;
                    cmd.Parameters.AddRange(parms);

                    SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);

                    this.Connection.Open();
                    int addedRowCount = da.Fill(dataTable);
                    this.Connection.Close();

                    return addedRowCount;
                }

                public int ExecuteNonQuery(string commandText, params DbParameter[] parms)
                {
                    this.Command.CommandText = commandText;
                    this.Command.Parameters.AddRange(parms);
                    
                    this.Connection.Open();
                    int affectedRowCount = this.Command.ExecuteNonQuery();
                    this.Connection.Close();

                    return affectedRowCount;
                }

                public DbParameter CreateParameter()
                {
                    return this.Command.CreateParameter();
                }

                public string ParameterModifier
                {
                    get { return "@"; }
                }


                public object ExecuteScalar(string commandText, params DbParameter[] parms)
                {
                    this.Command.CommandText = commandText;
                    this.Command.Parameters.AddRange(parms);

                    this.Connection.Open();
                    object value = this.Command.ExecuteScalar();
                    this.Connection.Close();

                    return value;
                }

                #endregion

            }
        }
    }
}